﻿/*
 Plugin for SmallJS toolkit
 Copyright (c) 2013 Walter M. Soto Reyes
 http://www.smalljs.com or https://smalljs.codeplex.com
 License: https://smalljs.codeplex.com/license
*/
(function ($) {

    var scrl = {
        scrolling: false,
        bind: function (onScrollEnd) {

            sj('[href^="#"]').forEach(function () { 
                var index = this.href.indexOf('#') + 1;
                var id = this.href.substring(index);
                this.href = '#';
                sj(this).onClick(function () {
                    scrl.to(id, onScrollEnd);
                });
            });

        },
        to: function (id,onScrollEnd) { 
            var o = sj(id).get[0];
            if (typeof o !== 'undefined') { 
                var point = smalljs.page.pos(o); 
                var c = smalljs.page.scrollOffSet(window); 
                scrl.scrolling = true;
                smalljs.ui.animate({
                    start: c.Y,
                    duration: 1000,
                    end: point.Y - 70,
                    onStep: function (i) {
                        window.scrollTo(0, i);
                    },
                    callback: function () {
                        scrl.scrolling = false;
                        if (typeof (onScrollEnd) === 'function') {
                            onScrollEnd(id);
                        } 
                    },
                    animation: smalljs.ui.animations.withSine
                });
            }
        }
    };


    

    smalljs.extend({
        scroll:scrl
    });


})(sj);